﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ScreenMonitor
{
    public abstract partial class AsycImageProviderBase : SmartImageProviderBase
    {

        #region========私有变量========

        private SmartImageViewer Viewer;
        private Task AsycOperateTask;
        private RectangleF LastReqCutImageRect;
        private SizeF LastReqCutImageSize;
        private SizeF LastReqGetPreviewImageSize;

        #endregion

        #region========私有函数========

        private void DoAsycOperate()
        {
            while (true)
            {
                this.StartGetImagePhysicalSize();
                this.StartGetPartImage(LastReqCutImageRect, LastReqCutImageSize);
                this.StartGetPreviewImage(LastReqGetPreviewImageSize);
                System.Threading.Thread.Sleep(AsycRefreshRate);
            }
        }

        #endregion

        #region========抽象函数========

        public abstract void StartGetPartImage(System.Drawing.RectangleF ViewRect, System.Drawing.SizeF ImageSize);
        public abstract void StartGetPreviewImage(System.Drawing.SizeF PreviewBoxSize);
        public abstract void StartGetImagePhysicalSize();

        #endregion

        #region==========属性==========

        private int AsycRefreshRateV = 1000;
        /// <summary>
        /// 获取或设置异步图片刷新的频率
        /// </summary>
        public int AsycRefreshRate
        {
            get { return AsycRefreshRateV; }
            set { AsycRefreshRateV = value; }
        }

        private Image CutImageV =new Bitmap(1,1);
        /// <summary>
        /// 表示暂存在内存中的最后一次异步取得的裁剪图片
        /// </summary>
        protected Image CutImage
        {
            private get { return CutImageV; }
            set
            {
                CutImageV = value;
                if (IsInited())
                    if (Viewer != null)
                        Viewer.Invalidate();
            }
        }

        private Image PreviewImageV = new Bitmap(1,1);
        /// <summary>
        /// 表示暂存在内存中的最后一次异步取得的浏览图片
        /// </summary>
        protected Image PreviewImage
        {
            private get { return PreviewImageV; }
            set
            {
                PreviewImageV = value;
                if (IsInited())
                    if (Viewer != null)
                        Viewer.Invalidate();
            }
        }

        private Size PhysicalSizeV = new Size(0, 0);
        /// <summary>
        /// 表示暂存在内存中的最后一次异步取得的物理大小图片
        /// </summary>
        protected Size PhysicalSize
        {
            private get { return PhysicalSizeV; }
            set
            {
                PhysicalSizeV = value;
                if (IsInited())
                    if (Viewer != null)
                        Viewer.Invalidate();
            }
        }

        private RectangleF LastAsycCutRectV;
        /// <summary>
        /// 表示最后一次异步裁剪图片的矩形区域
        /// </summary>
        protected RectangleF LastAsycCutRect
        {
            private get { return LastAsycCutRectV; }
            set
            {
                LastAsycCutRectV = value;
            }
        }


        #endregion

        #region========公有函数========

        public override System.Drawing.Image GetPreviewImage(System.Drawing.Size PreviewBoxSize)
        {
            LastReqGetPreviewImageSize = PreviewBoxSize;
            return PreviewImage;
        }

        public override System.Drawing.Image GetPartImage(System.Drawing.RectangleF ViewRect, System.Drawing.SizeF ImageSize)
        {
            LastReqCutImageSize = ImageSize;
            LastReqCutImageRect = ViewRect;
            if (CutImage == null || LastAsycCutRect != ViewRect || LastReqCutImageSize != ImageSize)
            {
                if (PreviewImage != null)
                {
                    Bitmap bmp = new Bitmap((int)ImageSize.Width, (int)ImageSize.Height);
                    Graphics g = Graphics.FromImage(bmp);
                    g.DrawImage(PreviewImage, Viewer.PreviewSelectRect, new Rectangle(0, 0, (int)ImageSize.Width, (int)ImageSize.Height), GraphicsUnit.Pixel);
                    g.Dispose();
                    return bmp;
                }
                else
                {
                    return new Bitmap((int)ImageSize.Width, (int)ImageSize.Height);
                }
            }
            else
            {
                return CutImage;
            }
        }

        public override System.Drawing.Size GetImagePhysicalSize()
        {
            return PhysicalSize;
        }

        public override bool IsInited()
        {
            if (PhysicalSize.Width > 0 && PhysicalSize.Height > 0 && PreviewImage != null)
                return true;
            else
                return false;
        }

        public override void Init(SmartImageViewer Viewer)
        {
            this.Viewer = Viewer;
            this.AsycOperateTask = new Task(DoAsycOperate);
            AsycOperateTask.Start();
        }

        #endregion


    }
}
